package com.woniu.darryringmall.front.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniu.darryringmall.front.entity.Address;
import com.woniu.darryringmall.front.entity.User;
import com.woniu.darryringmall.front.service.AddressService;
import com.woniu.darryringmall.utils.ResponseData;
import com.woniu.darryringmall.utils.ResultStateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 修改地址
     * @param address
     * @return
     */
    @PostMapping("update")
    public ResponseData update(@RequestBody Address address, HttpSession session){
        if((address.getIsDefault().equals("y"))){
            Map<String,Object> map = new HashMap<>();
            map.put("userid",((User)session.getAttribute("user")).getId());
            map.put("isdefault","y");
            addressService.update(new UpdateWrapper<Address>().allEq(map).set("isdefault","n"));
        }
        address.setUserId(((User)session.getAttribute("user")).getId());
        boolean flag = addressService.updateById(address);
        if(!flag){
            return new ResponseData(ResultStateCode.FAIL,"修改失败");
        }
        List<Address> addresses = addressService.list(new QueryWrapper<Address>().eq("userid", ((User)session.getAttribute("user")).getId()));
        return new ResponseData(addresses);
    }

    /**
     * 删除地址
     * @param id
     * @return
     */
    @PostMapping("delete")
    public ResponseData delete(int id,HttpSession session){
        boolean flag = addressService.removeById(id);
        if(!flag){
            return new ResponseData(ResultStateCode.FAIL,"删除失败");
        }
        List<Address> addresses = addressService.list(new QueryWrapper<Address>().eq("userid", ((User)session.getAttribute("user")).getId()));
        return new ResponseData(addresses);
    }


    /**
     * 添加地址
     * @param address
     * @return
     */
    @PostMapping("insert")
    public ResponseData add(@RequestBody Address address,HttpSession session){
        if((address.getIsDefault().equals("y"))){
            Map<String,Object> map = new HashMap<>();
            map.put("userid",((User)session.getAttribute("user")).getId());
            map.put("isdefault","y");
            addressService.update(new UpdateWrapper<Address>().allEq(map).set("isdefault","n"));
        }
        address.setUserId(((User)session.getAttribute("user")).getId());
        boolean flag = addressService.save(address);
        if(!flag){
            return new ResponseData(ResultStateCode.FAIL,"添加失败");
        }
        List<Address> addresses = addressService.list(new QueryWrapper<Address>().eq("userid", ((User)session.getAttribute("user")).getId()));
        return new ResponseData(addresses);
    }

}
